package com.ruoyi.category.controller;

import java.util.List;

import com.ruoyi.category.domain.vo.NexusCategoryVo;
import com.ruoyi.common.core.redis.RedisCache;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.category.domain.NexusCategory;
import com.ruoyi.category.service.INexusCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * app类别Controller
 *
 * @author Afanda
 * @date 2025-04-29
 */
@RestController
@RequestMapping("/NexusSphere/category")
public class NexusCategoryController extends BaseController {
    final String REDIS_KEY = "category";

    @Autowired
    private INexusCategoryService nexusCategoryService;

    @Autowired
    private RedisCache redisCache;

    /**
     * 查询app类别列表
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(NexusCategory nexusCategory) {
        startPage();
        List<NexusCategoryVo> list = nexusCategoryService.selectNexusCategoryVoList(nexusCategory);
        return getDataTable(list);
    }

    /**
     * 导出app类别列表
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:category:export')")
    @Log(title = "app类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NexusCategory nexusCategory) {
        List<NexusCategory> list = nexusCategoryService.selectNexusCategoryList(nexusCategory);
        ExcelUtil<NexusCategory> util = new ExcelUtil<NexusCategory>(NexusCategory.class);
        util.exportExcel(response, list, "app类别数据");
    }

    /**
     * 获取app类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(nexusCategoryService.selectNexusCategoryById(id));
    }

    /**
     * 新增app类别
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:category:add')")
    @Log(title = "app类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody NexusCategory nexusCategory) {
        Long categoryId = nexusCategory.getCategoryId();
        String key = REDIS_KEY + categoryId;
        redisCache.deleteObject(key);
        return toAjax(nexusCategoryService.insertNexusCategory(nexusCategory));
    }

    /**
     * 修改app类别
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:category:edit')")
    @Log(title = "app类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody NexusCategory nexusCategory) {
        Long categoryId = nexusCategory.getCategoryId();
        String key = REDIS_KEY + categoryId;
        redisCache.deleteObject(key);
        return toAjax(nexusCategoryService.updateNexusCategory(nexusCategory));
    }

    /**
     * 删除app类别
     */
    @PreAuthorize("@ss.hasPermi('NexusSphere:category:remove')")
    @Log(title = "app类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(nexusCategoryService.deleteNexusCategoryByIds(ids));
    }

    /**
     * 返回所有的名称
     */

    @GetMapping("/category/list")
    public AjaxResult getCategoryNameList() {
        return AjaxResult.success(nexusCategoryService.selectCategoryNameList());
    }
}
